﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NetUtilityLib;
using System.Collections;
using file;

namespace DMSMT.Tool
{
    public partial class FileCopyTool : Form
    {
        private string sourceDir;
        //private string tab;
        public string tab {get;set; }

        public FileCopyTool(string tab)
        {
            this.tab = tab;
            InitializeComponent();
        }

        private void btScan_Click(object sender, EventArgs e)
        {
            string dir = tbDir.Text;
            if (string.IsNullOrEmpty(dir))
            {
                MessageBox.Show("请选择要源文件夹");
                return;
            }
            if (dir.EndsWith("\\") || dir.EndsWith("/"))
                dir = dir.Substring(0, dir.Length - 1);
            lSourceFiles.Items.Clear();
            string pattern = tbFileType.Text;
            if (string.IsNullOrEmpty(pattern))
                pattern = "*";
            string date = dtUpdateDate.Text;
            DateTime d = DateTime.Now;
            if (!string.IsNullOrEmpty(date))
                d = DateTime.ParseExact(date, "yyyy-MM-dd HH:mm:ss", null);
            List<FileInfo> list = file.FileUtil.ListFiles(dir, d, pattern.Split(','));
            for (int i = 0; i < list.Count; i++)
            {
                FileInfo f = list[i];
                lSourceFiles.Items.Add(f);
            }
            this.sourceDir = dir;
        }

        private void btSelectDir_Click(object sender, EventArgs e)
        {
            string path = file.FileUtil.SelectDir();
            if (!string.IsNullOrEmpty(path))
                tbDir.Text = path;
        }

        private void FileCopyTool_Load(object sender, EventArgs e)
        {
            lSourceFiles.Items.Clear();
            InitForm();
        }

        private string GetAppStr(string key)
        {
            return Config.GetAppStr(key + "." + this.tab);
        }

        private void InitForm()
        {
            tbDir.Text = GetAppStr("tool.FileCopyTool.sourceDir");
            dtUpdateDate.Text = GetAppStr("tool.FileCopyTool.updateDate");
            tbFileType.Text = GetAppStr("tool.FileCopyTool.fileType");
            tbDesDir.Text = GetAppStr("tool.FileCopyTool.desDir");
        }

        private void FileCopyTool_FormClosing(object sender, FormClosingEventArgs e)
        {
            //SaveConfig();
        }

        private void SetAppStr(string key,string value)
        {
            Config.SetAppStr(key + "." + this.tab,value);
        }

        public void SaveConfig()
        {
            SetAppStr("tool.FileCopyTool.sourceDir", tbDir.Text);
            SetAppStr("tool.FileCopyTool.desDir", tbDesDir.Text);
            SetAppStr("tool.FileCopyTool.updateDate", dtUpdateDate.Text);
            SetAppStr("tool.FileCopyTool.fileType", tbFileType.Text);
        }

        private void FileCopyTool_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void btSelectDes_Click(object sender, EventArgs e)
        {
            string path = file.FileUtil.SelectDir();
            if (!string.IsNullOrEmpty(path))
                tbDesDir.Text = path;
        }

        private void btPub_Click(object sender, EventArgs e)
        {
            if (lSourceFiles.Items.Count == 0)
            {
                Log.LogWarning("没有可以发布的文件清单,请先扫描可以发布的文件清单!");
                return;
            }
            if (lSourceFiles.SelectedItems.Count == 0)
            {
                PubFiles(lSourceFiles.Items);
                lSourceFiles.ClearSelected();
            }
            else
                PubFiles(lSourceFiles.SelectedItems);
        }

        private void PubFiles(IList selectedItems)
        {
            string dir = tbDesDir.Text.Trim();
            if (string.IsNullOrEmpty(dir))
            {
                MessageBox.Show("请选择目标文件目录!");
                return;
            }
            //lPubFiles.Items.Clear();
            if (dir.EndsWith("\\") || dir.EndsWith("/"))
                dir = dir.Substring(0, dir.Length - 1);
            for (int i = 0; i < selectedItems.Count; i++)
            {
                FileInfo f = (FileInfo)selectedItems[i];
                DirectoryInfo d = new DirectoryInfo(this.sourceDir);

                string name = f.FullName.Substring(d.ToString().Length + 1);
                string desDir = dir + f.DirectoryName.Substring(d.ToString().Length);
                if (!Directory.Exists(desDir))
                {
                    Directory.CreateDirectory(desDir);
                }
                string desFile = desDir + "\\" + f.Name;
                try
                {
                    f.CopyTo(desFile, true);
                    lPubFiles.Items.Add(name + "\t" + f.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss"));
                    Log.LogText(f.FullName + "\t->\t" + desFile + "\t成功");
                }
                catch (Exception e)
                {
                    Log.LogError(e.Message);
                    Log.LogText(f.FullName + "\t->\t" + desFile + "\t失败");
                }
            }
        }

        private void btDDir_Click(object sender, EventArgs e)
        {
            FileUtil.DeleteDir(tbDesDir.Text.Trim());
        }

    }
}
